package com.redsoft.epip.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.redsoft.epip.entity.Goods;
import com.redsoft.epip.service.GoodsService;
import com.redsoft.spirit.entity.JsonResult;

/**
 * 后台商品管理的controller
 * 
 * <pre>
 * 后台商品管理的controller
 * </pre>
 * 
 * @author 杨雷
 * @since 1.0
 */
@RestController
@Validated
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	// 查询Goodslist
	@GetMapping("/**/goods")
	public String getJson(@RequestParam(value = "offset", required = false, defaultValue = "0") int offset,
			@RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {

		Map map = goodsService.getJson(offset, limit, searchKey);
		return new JsonResult(map).success().toString("yyyy-MM-dd HH:mm:ss");
	}

	// 保存或者修改
	@PostMapping("/**/goods")
	public String save(@RequestBody Goods goods) throws Exception {
		goodsService.save(goods);
		return new JsonResult().success().toString();
	}

	@DeleteMapping("/**/goods/{id}")
	public String delete(@PathVariable int id) {
		goodsService.delete(id);
		return new JsonResult().success().toString();
	}

	@GetMapping("/**/goods/{id}")
	public String getGoods(@PathVariable int id) {
		Goods goods = goodsService.get(id);
		return new JsonResult(goods).success().toString("yyyy-MM-dd HH:mm:ss");
	}
}
